import 'dart:convert';

import 'package:effective/model/speech_entity.dart';

speechEntityFromJson(SpeechVideoEntity data, Map<String, dynamic> json) {
  if (json['id'] != null) {
    data.id =
        json['id'] is String ? int.tryParse(json['id']) : json['id'].toInt();
  }
  if (json['title'] != null) {
    data.title = json['title'].toString();
  }
  if (json['speaker'] != null) {
    data.speaker = json['speaker'].toString();
  }
  if (json['gender'] != null) {
    data.gender = json['gender'] is String
        ? int.tryParse(json['gender'])
        : json['gender'].toInt();
  }
  if (json['district'] != null) {
    data.district = json['district'].toString();
  }
  if (json['contest'] != null) {
    data.contest = json['contest'].toString();
  }
  if (json['level'] != null) {
    data.level = json['level'] is String
        ? int.tryParse(json['level'])
        : json['level'].toInt();
  }
  if (json['rank'] != null) {
    data.rank = json['rank'] is String
        ? int.tryParse(json['rank'])
        : json['rank'].toInt();
  }
  if (json['theme'] != null) {
    data.theme = json['theme'].toString();
  }
  if (json['language'] != null) {
    data.language = json['language'].toString();
  }
  if (json['tags'] != null) {
    String temp = json['tags'].replaceFirst("\"", "");
    String end = temp;
    if (temp.length > 2) {
      int pos = temp.lastIndexOf("\"");
      end = temp.replaceRange(pos, pos + 1, "");
      end = end.replaceAll("\\", "");
    }
    print(jsonDecode(end).runtimeType);
    data.tags = (JsonDecoder().convert(end) as List)
        .map((v) => v.toString())
        .toList()
        .cast<String>();
  }
  if (json['video'] != null) {
    data.video = json['video'].toString();
  }
  if (json['description'] != null) {
    data.description = json['description'].toString();
  }
  if (json['highlights'] != null) {
    String temp = json['highlights'].replaceFirst("\"", "");
    String end = temp;
    if (temp.length > 2) {
      int pos = temp.lastIndexOf("\"");
      end = temp.replaceRange(pos, pos + 1, "");
      end = end.replaceAll("\\", "");
      String res = end;
      for (int i = 0; i < end.length; i++) {
        if(res[i]!="]"){
          if (res[i] == '{') {
            res = res.replaceRange(i - 1, i, "");
          } else if (res[i]  == '}') {
            res = res.replaceRange(i + 1, i + 2, "");
          }
        }else{
          break;
        }
      }
      end = res;
    }
    data.highlights = (JsonDecoder().convert(end) as List)
        .map((v) => v.toString())
        .toList()
        .cast<String>();
  }
  if (json['createTime'] != null) {
    data.createTime = json['createTime'].toString();
  }
  if (json['lastUpdateTime'] != null) {
    data.lastUpdateTime = json['lastUpdateTime'].toString();
  }
  if (json['status'] != null) {
    data.status = json['status'] is String
        ? int.tryParse(json['status'])
        : json['status'].toInt();
  }
  if (json['creator'] != null) {
    data.creator = json['creator'] is String
        ? int.tryParse(json['creator'])
        : json['creator'].toInt();
  }
  return data;
}

Map<String, dynamic> speechEntityToJson(SpeechVideoEntity entity) {
  final Map<String, dynamic> data = new Map<String, dynamic>();
  data['speechId'] = entity.id;
  data['title'] = entity.title;
  data['speaker'] = entity.speaker;
  data['gender'] = entity.gender;
  data['district'] = entity.district;
  data['contest'] = entity.contest;
  data['level'] = entity.level;
  data['rank'] = entity.rank;
  data['theme'] = entity.theme;
  data['tags'] = JsonEncoder().convert(entity.tags);
  data['video'] = entity.video;
  data['description'] = entity.description;
  data['highlights'] = JsonEncoder().convert(entity.highlights);
  data['createTime'] = entity.createTime;
  data['lastUpdateTime'] = entity.lastUpdateTime;
  data['status'] = entity.status;
  data['creator'] = entity.creator;
  data['language'] = entity.language;
  return data;
}
